﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class TradeHistoryReportForm : DevExpress.XtraEditors.XtraForm
    {
        public TradeHistoryReportForm()
        {
            InitializeComponent();
            dateEdit_dateStart.DateTime = DateTime.Now.AddDays(-7);
            dateEdit_dateEnd.DateTime = DateTime.Now;

            btn_select.Enabled = false;
            _thread = new Thread(BindData);
            _thread.Start();
            
        }

        private Thread _thread;

        private void btn_select_Click(object sender, EventArgs e)
        {
            if(dateEdit_dateStart.DateTime >dateEdit_dateEnd.DateTime)
            {
                XtraMessageBox.Show("开始日期不能大于结束日期！");
                dateEdit_dateStart.Focus();
                return;
            }
            btn_select.Enabled = false;
            _thread = new Thread(BindData);
            _thread.Start();
        }

        private void BindData()
        {
            string startTime = dateEdit_dateStart.DateTime.ToString("yyyy-MM-dd") 
                + " 00:00:00";
            string endTime = dateEdit_dateEnd.DateTime.ToString("yyyy-MM-dd") 
                + " 23:59:59";
            string sql = "tradetime between '" + startTime + "' and '" + endTime+"'";
            string sql2 = "calltime between '" + startTime + "' and '" + endTime + "'";      
            try
            {
                DataSet ds = MainForm.PositionService.TradeGetReport(sql);              
                if(ds!=null)
                {
                    #region ----- 将平台拨出记录加入-----
                    DataSet ds2 = MainForm.PositionService.CallRecordsGetReportByDay(sql2);
                    //若有平台拨出记录
                    if (ds2 != null && ds2.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row2 in ds2.Tables[0].Rows)
                        {
                            string where = "DT='" + row2["DT"] + "'";
                            //找出符合平台记录日期的 交易数据
                            DataRow[] rows = ds.Tables[0].Select(where);
                            //若该日期有交易数据 将平台拨出记录 加入交易数据
                            if (rows.Length == 1)
                            {
                                int total = int.Parse(rows[0]["Total"].ToString());
                                int plat = int.Parse(rows[0]["Platform"].ToString());
                                int plat2 = int.Parse(row2["platOut"].ToString());
                                //int taxi = int.Parse(row2["DrCallIn"].ToString());
                                total += plat2;
                                plat += plat2;
                                rows[0]["Total"] = total;
                                rows[0]["Platform"] = plat;
                                rows[0]["Taxi"] = row2["DrCallIn"];
                            }
                            //若该日期没有交易数据 则将平台拨出记录记为交易数据
                            else
                            {
                                DataRow row = ds.Tables[0].NewRow();
                                row["DT"] = row2["DT"];
                                row["Total"] = row2["platOut"];
                                row["Taxi"] = row2["DrCallIn"];
                                row["Passenger"] = "0";
                                row["Platform"] = row2["platOut"];
                                ds.Tables[0].Rows.Add(row);
                            }

                        }
                    }
                    #endregion

                    #region  加入每天在线出租车数量
                    ds.Tables[0].Columns.Add("DrOnlineCount",typeof(Int32));
                    string sql3 = "updatetime between '" + startTime + "' and '" + endTime + "'";         
                    DataSet ds3 = MainForm.PositionService.GetTaxiOnlineCountReport(sql3);                    
                    if (ds3 != null && ds3.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            string where = "DT='" + row["DT"] + "'";
                            //找出各日期出租车在线数量
                            DataRow[] rows = ds3.Tables[0].Select(where);
                            //若该日期有交易数据 将平台拨出记录 加入交易数据
                            if (rows.Length == 1)
                            {
                                row["DrOnlineCount"] = rows[0]["DrCount"];
                            }
                            //若该日期没有交易数据 则将平台拨出记录记为交易数据
                            else
                            {
                                row["DrOnlineCount"] = 0;
                            }

                        }
                    }

                    #endregion

                    BeginInvoke(new MethodInvoker(() =>
                     {
                        ds.Tables[0].DefaultView.Sort = "DT asc";
                        chartControl1.DataSource = ds.Tables[0];
                        //diagram.AxisY.Title.Text = "用户数量(人)";
                        chartControl1.Series[0].ArgumentDataMember = "DT";
                        chartControl1.Series[0].ValueDataMembers.AddRange(new string[] { "DrOnlineCount" });
                        chartControl1.Series[1].ArgumentDataMember = "DT";
                        chartControl1.Series[1].ValueDataMembers.AddRange(new string[] { "Taxi" });
                        chartControl1.Series[2].ArgumentDataMember = "DT";
                        chartControl1.Series[2].ValueDataMembers.AddRange(new string[] { "Passenger" });
                        chartControl1.Series[3].ArgumentDataMember = "DT";
                        chartControl1.Series[3].ValueDataMembers.AddRange(new string[] { "Platform" });
                        btn_select.Enabled = true;
                    })); 
                }
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);                
            }
            
                  
        }
        

        private void TradeHistoryReportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_thread != null)
            {
                _thread.Abort();
            }
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion
    }
}